﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DBService.ViewDatas;
using DBService;

public partial class CompanyRatings : System.Web.UI.Page
{
    int iCount = 1;
    //int PageSize = 10;
    //int ItemCount;
    //int PagesCount;
    ModelAdapter service = new ModelAdapter();
    int SortCat = 0;
    //List<CompanyRatingInfo> coll;

    protected void Page_Load(object sender, EventArgs e)
    {
        iCount = 1;
        SortCat = 1;
        BindRepeater();
    }

    protected void TopOfWeek_Click(object sender, EventArgs e)
    {
        //coll = service.GetCompanyRatingsInfo().OrderByDescending(c => c.WebSite).Take(20).ToList();
        SortCat = 1;
        BindRepeater();
    }

    protected void NEWLY_Click(object sender, EventArgs e)
    {
        //coll = service.GetCompanyRatingsInfo().OrderByDescending(c => c.CreateDate).Take(20).ToList();
        SortCat = 2;
        BindRepeater();
    }

    protected void MostDiscussed_Click(object sender, EventArgs e)
    {
        //coll = service.GetCompanyRatingsInfo().OrderByDescending(c => c.ReviewsCount).Take(20).ToList();
        SortCat = 3;
        BindRepeater();
    }

    protected void Total_Click(object sender, EventArgs e)
    {
        //coll = service.GetCompanyRatingsInfo().OrderByDescending(c => c.Rating).Take(20).ToList();
        SortCat = 4;
        BindRepeater();
    }

    public void Repeater1_databount(Object Sender, RepeaterItemEventArgs e)
    {
        var item = (CompanyRatingInfo)e.Item.DataItem;
        if (item != null)
        {
            var lit = (Literal)e.Item.FindControl("CompanyItem");

            string divClass = iCount % 2 == 1 ? "rating_watch" : "rating_watch_one";
            string logo = "upload/" + (string.IsNullOrEmpty(item.Logo) ? "no-company-logo.jpg" : item.Logo);


            lit.Text = "<div class=\"" + divClass + "\"/><div class=\"raitng_info\"><h1>" + iCount.ToString("00")
            + "</h1><a href=\"singlecompany.aspx?id=" + item.CompanyID + "\"><span>" + item.CompanyName + "</span></a><br />" +
            "<div class=\"user_rat\">" +
                "<div class=\"outer-star\">" +
                    "<div id=\"rateing\" style=\"width:" +((int)((item.Rating) * 20)).ToString() + "%\" class=\"inner-star\"></div></div></div>" +
            "<div class=\"chat_txt\"/>" + item.Rating.ToString("F2") +
            "</div><a class=\"chat_icon\" href=\"singlecompany.aspx?id=" + item.CompanyID + "\">" + item.ReviewsCount + "</a>"
            + "</div><div class=\"logo_div\"><a href=\"singlecompany.aspx?id=" + item.CompanyID + "\"><img src=\"" + logo + "\" width=\"63\" height=\"50\" /></a>"
            + "</div></div>";
            iCount++;
        }
    }

    private void BindRepeater()
    {
        List<CompanyRatingInfo> coll;
        iCount = 1;
        switch (SortCat)
        {
            case 1:
                coll = service.GetTopCompanies();
                //coll = service.GetCompanyRatingsInfo().OrderByDescending(c => c.WebSite).Take(20).ToList();
                //coll = service.GetTopCompanies(null).Select(c => new CompanyRatingInfo()
                //{
                //    CompanyID = c.CompanyID,
                //    CompanyName = c.Name,
                //    Logo = c.Logo,
                //    Rating = (double)((c.sumrating) ?? 4.0m),
                //    ReviewsCount = 13,
                //    WebSite = c.Title,
                //    CreateDate = DateTime.Now.AddDays(-20)
                //}).ToList();
                break;
            case 2:
                coll = service.GetCompanyRatingsInfo().OrderByDescending(c => c.CreateDate).Take(20).ToList();
                break;
            case 3:
                coll = service.GetCompanyRatingsInfo().OrderByDescending(c => c.ReviewsCount).Take(20).ToList();
                break;
            case 4:
                coll = service.GetCompanyRatingsInfo().OrderByDescending(c => c.Rating).Take(20).ToList();
                break;
            default:
                coll = service.GetCompanyRatingsInfo();
                break;
        }
        Repeater1.DataSource = coll;
        Repeater1.DataBind();
        /*
        ItemCount = coll.Count;

        PagedDataSource Pds1 = new PagedDataSource();
        Pds1.DataSource = coll;
        Pds1.AllowPaging = true;
        Pds1.PageSize = coll.Count();
        Repeater1.DataSource = Pds1;
        Repeater1.DataBind();

        var lastIndex = PagesCount = ItemCount % PageSize == 0 ? (ItemCount / PageSize) : (ItemCount / PageSize) + 1;
        var startIndex = 1;

        if (lastIndex - startIndex > 0)
        {
            Pds1.CurrentPageIndex = CurrentPage;
            pages.Controls.Clear();
            for (int i = startIndex; i <= lastIndex; i++)
            {
                var lb = new LinkButton() { Text = i.ToString(), CssClass = "page_bg_num" };
                lb.ID = "btnlink" + i.ToString();
                lb.CssClass = "page_bg_num";
                lb.Click += new EventHandler(lb_Click);
                pages.Controls.Add(lb);
            }
            if (CurrentPage != lastIndex - 1)
            {
                // show next
                var lb1 = new LinkButton() { Text = "►", CssClass = "page_bg_num" };
                lb1.ID = "btnlink-1";
                lb1.Click += new EventHandler(lb_Click);
                pages.Controls.Add(lb1);

                // show last
                var lblast = new LinkButton() { Text = "ბოლო", CssClass = "page_bg_num" };
                lblast.ID = "lblast100";
                lblast.CssClass = "page_last";
                lblast.Click += new EventHandler(lb_Click);
                pages.Controls.Add(lblast);
            }
        }
        */
    }
    /*
    protected void lb_Click(object sender, EventArgs e)
    {
        var lb = (LinkButton)sender;
        if (lb.Text == "ბოლო")
        {
            CurrentPage = 10000;
        }
        else if (lb.Text == "►")
        {
            CurrentPage += 1;
        }
        else
        {
            CurrentPage = int.Parse(lb.Text) - 1;
        }
        BindRepeater();
    }

    public int CurrentPage
    {
        get
        {
            object s1 = this.ViewState["CurrentPage"];
            if (s1 == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(s1);
            }
        }
        set
        {
            if (value > PagesCount - 1)
            {
                value = PagesCount - 1;
            }
            this.ViewState["CurrentPage"] = value;
        }
    }
    */
}